package com.tk.exam.utils;


import com.alibaba.fastjson.JSONObject;
import com.tk.exam.domain.TkOperator;
import com.tk.exam.domain.TkStudent;
import com.tk.exam.domain.TkToken;
import com.tk.exam.service.TkOperatorServer;
import com.tk.exam.service.TkStudentServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class TokenThreadUtil {

    @Autowired
    TkStudentServer tkStudentServer;
    @Autowired
    TkOperatorServer tkOperatorServer;

    private static final ThreadLocal<TkToken> tokenThreadLocal = new ThreadLocal<>();

    public static TkToken getTkToken() {
        TkToken tkToken = tokenThreadLocal.get();
        return tkToken;
    }

    public static void setTkToken(TkToken tkToken) {
        tokenThreadLocal.set(tkToken);
    }

    public TkOperator getTkOperator() {
        TkToken tkToken = getTkToken();
        Map<String, Object> tokenInfoMap = JSONObject.parseObject(tkToken.getTokenInfo());
        String username = (String) tokenInfoMap.get("username");
        return tkOperatorServer.getByLoginName(username);
    }


}
